package main

import (
	"github.com/julienschmidt/httprouter"
	"html/template"
	"io"
	"log"
	"net/http"
	"os"
	"time"
)

func testPageHandler(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	t, _ := template.ParseFiles("./videos/upload.html")
 
    t.Execute(w, nil)
}

func streamHandler(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	vid := p.ByName("vid-id")
	vl := VIDEO_DIR + vid

	video, err := os.Open(vl)
	if err != nil {
		log.Printf("Error when try to open file: %v", err)
		sendErrorResponse(w, http.StatusInternalServerError, "Internal Error")
		return
	}

	w.Header().Set("Content-Type", "video/mp4")
	http.ServeContent(w, r, "", time.Now(), video)

	defer video.Close()
}

//func streamHandler(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
//	log.Println("Entered the streamHandler")
//	targetUrl := "http://avenssi-videos2.oss-cn-qingdao.aliyuncs.com/videos/" + p.ByName("vid-id")
//	http.Redirect(w, r, targetUrl, 301)
//}

func uploadHandler(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	r.Body = http.MaxBytesReader(w, r.Body, MAX_UPLOAD_SIZE)
	if err := r.ParseMultipartForm(MAX_UPLOAD_SIZE); err != nil {
		log.Printf("Error when try to ParseMultipartForm: %v", err)
		sendErrorResponse(w, http.StatusBadRequest, "File is too big")
		return
	}


	file, _, err := r.FormFile("file")
	if err != nil {
		log.Printf("Error when try to get file: %v", err)
		sendErrorResponse(w, http.StatusInternalServerError, "Internal Error108")
		return
	}
	defer file.Close()
	fn := p.ByName("vid-id")
	f, err := os.Create(VIDEO_DIR + fn)
	defer f.Close()
	ioc, err := io.Copy(f, file)
	if err != nil {
		log.Printf("Error when try to get file qinui: %v -- %v", ioc, err)
		sendErrorResponse(w, http.StatusInternalServerError, "Internal Error118")
		return
	}
	w.WriteHeader(http.StatusCreated)
	io.WriteString(w, "Uploaded successfully")
}


//func uploadHandler(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
//	r.Body = http.MaxBytesReader(w, r.Body, MAX_UPLOAD_SIZE)
//	if err := r.ParseMultipartForm(MAX_UPLOAD_SIZE); err != nil {
//		sendErrorResponse(w, http.StatusBadRequest, "File is too big")
//		return
//	}
//
//	file, _, err := r.FormFile("file")
//	if err != nil {
//		log.Printf("Error when try to get file: %v", err)
//		sendErrorResponse(w, http.StatusInternalServerError, "Internal Error")
//		return
//	}
//
//	data, err := ioutil.ReadAll(file)
//	if err != nil {
//		log.Printf("Read file error: %v", err)
//		sendErrorResponse(w, http.StatusInternalServerError, "Internal Error")
//	}
//
//	fn := p.ByName("vid-id")
//	err = ioutil.WriteFile(VIDEO_DIR + fn, data, 0666)
//	if err != nil {
//		log.Printf("Write file error: %v", err)
//		sendErrorResponse(w, http.StatusInternalServerError, "Internal Error")
//		return
//	}
//
//	ossfn := "videos/" + fn
//	path := "./videos/" + fn
//	bn := "avenssi-videos2"
//	ret := UploadToOss(ossfn, path, bn)
//	if !ret {
//		sendErrorResponse(w, http.StatusInternalServerError, "Internal Error")
//		return
//	}
//
//	os.Remove(path)
//
//	w.WriteHeader(http.StatusCreated)
//	io.WriteString(w, "Uploaded successfully")
//}
